package com.bawei.module_wide.view.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.viewpager.widget.PagerAdapter
import com.bawei.module_base.net.bean.wide.FindRecommendConstellationBean
import com.bawei.module_wide.R
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions


class FindCommendAdapter(
    val context: Context,
    val list: List<FindRecommendConstellationBean>
) : PagerAdapter() {
    //星座map集合
    private val mapImp = mapOf(
        "白羊座" to R.mipmap.sign1_01,
        "金牛座" to R.mipmap.sign1_02,
        "双子座" to R.mipmap.sign1_03,
        "巨蟹座" to R.mipmap.sign1_04,
        "狮子座" to R.mipmap.sign1_05,
        "处女座" to R.mipmap.sign1_06,
        "天秤座" to R.mipmap.sign1_07,
        "天蝎座" to R.mipmap.sign1_08,
        "射手座" to R.mipmap.sign1_09,
        "摩羯座" to R.mipmap.sign1_10,
        "水瓶座" to R.mipmap.sign1_11,
        "双鱼座" to R.mipmap.sign1_12,
    )

    override fun getCount() = list.size

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    @SuppressLint("MissingInflatedId")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        //获取到对象
        val resultBean = list.get(position)
        //获取布局
        val view = LayoutInflater.from(context).inflate(R.layout.item, null)
        //获取背景图
        val imgBg = view.findViewById<ImageView>(R.id.item_find_commend_bg_id)
        Glide.with(context).load(resultBean.personalImage)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
            .into(imgBg)
        //获取主播名称
        val name = view.findViewById<TextView>(R.id.item_find_name_id)
        name.setText(resultBean.nickName)
        //获取性别
        val sex = view.findViewById<ImageView>(R.id.item_find_sex_id)
        //性别 1=男 2=女
        if (resultBean.sex == 1) {
            sex.setImageResource(R.mipmap.man)
        } else {
            sex.setImageResource(R.mipmap.woman)
        }
        //星座
        val constellation = view.findViewById<ImageView>(R.id.item_find_constellation_id)
        //使用map寻找到对应的map的value值图片
        constellation.setImageResource(mapImp.getValue(resultBean.constellation))
        //标签
        val tabL = view.findViewById<TextView>(R.id.item_find_tab_id)
        //关注了
        val focusStatusTrue =
            view.findViewById<ConstraintLayout>(R.id.item_find_focusStatus_true_id)
        //取消关注
        val focusStatusFalse =
            view.findViewById<ConstraintLayout>(R.id.item_find_focusStatus_false_id)
        //是否关注 1=未关注 2=已关注
        if (resultBean.focusStatus == 1) {
            focusStatusTrue.visibility = View.VISIBLE
            focusStatusFalse.visibility = View.GONE
        } else {
            focusStatusTrue.visibility = View.GONE
            focusStatusFalse.visibility = View.VISIBLE
        }
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE;
    }

}